package com.dm.cloud.images.controller.defalut;

import com.dm.cloud.core.mode.ResultModel;
import com.dm.cloud.images.mode.fileupload.*;
import com.dm.cloud.oauthlib.service.anon.HasLogin;
import com.dm.cloud.tkmybatis.service.BaseEntityService;
import com.dm.cloud.tkmybatis.service.EntityBaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.dm.cloud.images.common.po.ImagesShard;
import com.dm.cloud.images.common.dto.ImagesShardDTO;
import com.dm.cloud.images.common.request.ImagesShardSearchRequest;
import com.dm.cloud.images.service.ImagesShardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
   * @author hcx
   * @type ImagesShardController.java
   * @desc 共享配置项 ImagesShard controller
   */
@RestController
@RequestMapping("/apis/v1/imagesShard")
@Tag(name =  "共享配置项  ImagesShardcontroller")
@HasLogin
public class ImagesShardController extends EntityBaseController<ImagesShardSearchRequest, ImagesShard, ImagesShardDTO> {


    @Autowired
    private ImagesShardService imagesShardService;
    @Override
    public ImagesShard getEntity() {
        return new ImagesShard();
    }

    @Override
    public ImagesShardDTO getDTO() {
        return new ImagesShardDTO();
    }

    @Override
    public BaseEntityService<ImagesShardSearchRequest, ImagesShard> getService() {
        return imagesShardService;
    }

    @PostMapping("shardStart")
    @Operation(summary = "分片上传文件第一步：获取上传文件标识")
    public ResultModel<FileUploadShardStartResultMode> shardStart(@RequestBody FileUploadShardStartMode fileUploadShardStartMode){
        FileUploadShardStartResultMode re=imagesShardService.shardStart(fileUploadShardStartMode);
        return ResultModel.success(re);
    }

    @PostMapping("shardUpload")
    @Operation(summary = "上传分片文件第二步：上传分片。 fileKey:第一个步骤返回的key, shardNum:当前分片编号 ")
    public ResultModel<Boolean> shardUpload(MultipartFile file, String fileKey, Integer shardNum){
        Boolean isOk= imagesShardService.shardUpload(file,fileKey,shardNum);
//        if(isOk){
//            throw new CustomException("测试分片失败！");
//        }
        return ResultModel.success(isOk);
    }

    @PostMapping("shardMerge")
    @Operation(summary = "上传分片文件第三步：合并分片。返回相对路径")
    public ResultModel<String> shardMerge(@RequestBody FileUploadMerge fileUploadMerge){
        String filePath= imagesShardService.shardMerge(fileUploadMerge);
        return ResultModel.success(filePath);
    }

    @PostMapping("shardDir")
    @Operation(summary = "在共享目录中创建文件夹")
    public ResultModel<String> shardDir(@RequestBody FileDirMkdir fileMkdir){
        String filePath= imagesShardService.shardDir(fileMkdir);
        return ResultModel.success(filePath);
    }

    @GetMapping("shardFiles")
    @Operation(summary = "获取分享目录下的所有文件名")
    public ResultModel<List<FileMode>> shardFiles(
            @Parameter(in = ParameterIn.QUERY,description = "分享目录标识"
            ) String shardId){
        List<FileMode> files=imagesShardService.shardFiles(shardId);
        return ResultModel.success(files);
    }

    @GetMapping("getEditContent")
    @Operation(summary = "获取配置文件内容")
    public ResultModel<String> getEditContent(
            @Parameter(in = ParameterIn.QUERY,description = "文件路径"
            ) String filePath,
            @Parameter(in = ParameterIn.QUERY,description = "共享目录标识")String shardId,
            @Parameter(in = ParameterIn.QUERY,description = "字符编码")String charCode){
        String fileContent=imagesShardService.getEditContent(shardId,filePath,charCode);
        ResultModel re = ResultModel.success("成功");
        re.setData(fileContent);
        return re;
    }

    @PostMapping("saveEditContent")
    @Operation(summary = "保存配置文件内容")
    public ResultModel<Boolean> saveEditContent(@RequestBody SaveEditContentMode saveEditContentMode){
        Boolean isSave=imagesShardService.saveEditContent(saveEditContentMode);
        return ResultModel.success(isSave);
    }

    @PostMapping("deleteFile")
    @Operation(summary = "删除文件/目录")
    public ResultModel<Boolean> deleteFile(@RequestBody DeleteFileModel deleteFileModel){
        Boolean isDelete= imagesShardService.deleteFile(deleteFileModel);
        return ResultModel.success(isDelete);
    }

    @GetMapping("export")
    @Operation(summary = "导出:目录导出压缩包，单个文件则导出文件")
    public void export(
            @Parameter(in=ParameterIn.QUERY,description = "共享标识")String shardId
            , @Parameter(in=ParameterIn.QUERY,description = "文件路径：为空或者 / 则代表导出整个共享目录")String filePath
            , HttpServletResponse response
            ){
        imagesShardService.export(shardId,filePath,response);
    }

}
